package org.jeecg.modules.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.modules.report.entity.ReportConfig;
import org.jeecg.modules.report.entity.ReportConfigInfo;
import org.jeecg.modules.report.entity.ReportConfigVideo;
import org.jeecg.modules.report.mapper.ReportConfigInfoMapper;
import org.jeecg.modules.report.mapper.ReportConfigMapper;
import org.jeecg.modules.report.mapper.ReportConfigVideoMapper;
import org.jeecg.modules.report.service.IReportConfigService;
import org.jeecg.modules.report.vo.StationBean;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 战图显示配置
 * @Author: jeecg-boot
 * @Date:   2019-08-20
 * @Version: V1.0
 */
@Service
public class ReportConfigServiceImpl extends ServiceImpl<ReportConfigMapper, ReportConfig> implements IReportConfigService {

	@Autowired
	private ReportConfigMapper reportConfigMapper;
	@Autowired
	private ReportConfigInfoMapper reportConfigInfoMapper;
	@Autowired
	private ReportConfigVideoMapper reportConfigVideoMapper;

	@Override
	public List<ReportConfig> findConfigPage(IPage<ReportConfig> page, ReportConfig reportConfig) {
		return reportConfigMapper.findConfigPage(page,reportConfig);
	}

	@Override
	@Transactional
	public void saveMain(ReportConfig reportConfig, List<ReportConfigInfo> reportConfigInfoList,List<ReportConfigVideo> reportConfigVideoList) {
		reportConfigMapper.insert(reportConfig);
		for(ReportConfigInfo entity:reportConfigInfoList) {
			//外键设置
			entity.setConfigId(reportConfig.getId());
			reportConfigInfoMapper.insert(entity);
		}
		for(ReportConfigVideo entity:reportConfigVideoList) {
			//外键设置
			entity.setConfigId(reportConfig.getId());
			reportConfigVideoMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(ReportConfig reportConfig,List<ReportConfigInfo> reportConfigInfoList,List<ReportConfigVideo> reportConfigVideoList) {
		reportConfigMapper.updateById(reportConfig);

		//1.先删除子表数据
		ReportConfigInfo reportConfigInfo = new ReportConfigInfo();
		reportConfigInfo.setConfigId(reportConfig.getId());
		reportConfigInfoMapper.deleteByMainId(reportConfigInfo);


		reportConfigVideoMapper.deleteByMainId(reportConfig.getId());

		//2.子表数据重新插入
		for(ReportConfigInfo entity:reportConfigInfoList) {
			//外键设置
			entity.setConfigId(reportConfig.getId());
			reportConfigInfoMapper.insert(entity);
		}
		for(ReportConfigVideo entity:reportConfigVideoList) {
			//外键设置
			entity.setConfigId(reportConfig.getId());
			reportConfigVideoMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		ReportConfigInfo reportConfigInfo = new ReportConfigInfo();
		reportConfigInfo.setConfigId(id);
		reportConfigInfoMapper.deleteByMainId(reportConfigInfo);
		ReportConfig reportConfig = new ReportConfig();
		reportConfig.setId(id);
		reportConfigMapper.deleteById(reportConfig);
		ReportConfigVideo reportConfigVideo = new ReportConfigVideo();
		reportConfigVideo.setId(id);
		reportConfigVideoMapper.deleteById(reportConfigVideo);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			ReportConfigInfo reportConfigInfo = new ReportConfigInfo();
			reportConfigInfo.setConfigId(id.toString());
			reportConfigInfoMapper.deleteByMainId(reportConfigInfo);
			reportConfigMapper.deleteById(id);
		}
	}

	@Override
	public List<ReportConfig> findConfigByProjectId(String type, String projectId) {
		return reportConfigMapper.findConfigByProjectId(type,projectId);
	}

	@Override
	public List<ReportConfig> findConfigByDepartIdOnly(String type, String departId) {
		return reportConfigMapper.findConfigByDepartIdOnly(type,departId);
	}

	@Override
	public List<ReportConfig> findAll() {
		return reportConfigMapper.findAll();
	}

	@Override
	public int saveBatch(List<ReportConfig> reportConfigList) {
		return reportConfigMapper.saveBatch(reportConfigList);
	}

	@Override
	public List<ReportConfig> findListByUser(StationBean params) {
		return reportConfigMapper.findListByUser(params);
	}

	@Override
	public List<ReportConfigInfo> findConfigInfoByProjectId(String type, String projectId) {
		return reportConfigInfoMapper.findConfigInfoByProjectId(type,projectId);
	}



}
